<template>
  <div class="membership-card" :class="{ 'premium': isPremium, 'expired': isExpired }">
    <div class="card-header">
      <div class="member-badge" v-if="isPremium">
        <svg width="20" height="20" viewBox="0 0 24 24" fill="none" class="crown-icon">
          <path d="M12 2L15.09 8.26L22 9.27L17 14.14L18.18 21.02L12 17.77L5.82 21.02L7 14.14L2 9.27L8.91 8.26L12 2Z" fill="#ffd700"/>
        </svg>
        {{ membershipLevelName }}
      </div>
      <div class="member-badge basic" v-else>
        普通用户
      </div>
    </div>
    
    <div class="card-body">
      <div class="member-info">
        <div class="member-avatar">
          <img :src="userAvatar" :alt="username" />
        </div>
        <div class="member-details">
          <h3 class="username">{{ username }}</h3>
          <p class="member-status" v-if="isPremium">
            {{ isExpired ? '会员已过期' : '会员有效期至: ' + formatDate(expiryDate) }}
          </p>
        </div>
      </div>
      
      <div class="member-stats" v-if="isPremium">
        <div class="stat-item">
          <span class="stat-label">会员等级</span>
          <span class="stat-value">{{ membershipLevel }}</span>
        </div>
        <div class="stat-item">
          <span class="stat-label">积分</span>
          <span class="stat-value">{{ points }}</span>
        </div>
        <div class="stat-item">
          <span class="stat-label">有效期</span>
          <span class="stat-value">{{ daysRemaining }}天</span>
        </div>
      </div>
    </div>
    
    <div class="card-footer" v-if="!isPremium || isExpired">
      <router-link to="/membership/upgrade" class="upgrade-btn">
        立即开通会员
      </router-link>
    </div>
    
    <div class="card-footer" v-else>
      <button class="renew-btn" @click="renewMembership">
        续费会员
      </button>
    </div>
  </div>
</template>

<script setup>
import { computed } from 'vue';
import { userState } from '../store.js';

const props = defineProps({
  userAvatar: {
    type: String,
    default: ''
  },
  username: {
    type: String,
    default: ''
  },
  membershipLevel: {
    type: Number,
    default: 0
  },
  expiryDate: {
    type: String,
    default: ''
  },
  points: {
    type: Number,
    default: 0
  }
});

const emit = defineEmits(['renew']);

const isPremium = computed(() => props.membershipLevel > 0);
const isExpired = computed(() => {
  if (!props.expiryDate) return true;
  const expiry = new Date(props.expiryDate);
  return expiry < new Date();
});

const daysRemaining = computed(() => {
  if (!isPremium.value || isExpired.value) return 0;
  const expiry = new Date(props.expiryDate);
  const now = new Date();
  const diffTime = Math.abs(expiry - now);
  return Math.ceil(diffTime / (1000 * 60 * 60 * 24));
});

const membershipLevelName = computed(() => {
  const level = props.membershipLevel;
  switch(level) {
    case 1: return '黄金会员';
    case 2: return '铂金会员';
    case 3: return '钻石会员';
    default: return '普通用户';
  }
});

const formatDate = (dateString) => {
  if (!dateString) return '';
  const date = new Date(dateString);
  return date.toLocaleDateString('zh-CN');
};

const renewMembership = () => {
  emit('renew');
};
</script>

<style scoped>
.membership-card {
  background: white;
  border-radius: 12px;
  padding: 20px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
  max-width: 400px;
  margin: 0 auto;
}

.membership-card.premium {
  border: 2px solid #ffd700;
}

.membership-card.expired {
  border: 2px solid #ccc;
}

.card-header {
  display: flex;
  justify-content: flex-end;
  margin-bottom: 16px;
}

.member-badge {
  display: flex;
  align-items: center;
  gap: 6px;
  background: linear-gradient(135deg, #ffd700, #ffed4a);
  color: #7b6800;
  padding: 4px 12px;
  border-radius: 20px;
  font-weight: bold;
  font-size: 14px;
}

.member-badge.basic {
  background: #f0f0f0;
  color: #666;
}

.crown-icon {
  animation: rotate 4s linear infinite;
}

@keyframes rotate {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
}

.card-body {
  margin-bottom: 20px;
}

.member-info {
  display: flex;
  align-items: center;
  gap: 16px;
  margin-bottom: 20px;
}

.member-avatar {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  overflow: hidden;
  border: 2px solid #ffd700;
}

.member-avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.member-details h3 {
  margin: 0 0 4px 0;
  font-size: 18px;
}

.member-status {
  margin: 0;
  font-size: 14px;
  color: #666;
}

.member-stats {
  display: flex;
  justify-content: space-between;
  background: #f9f9f9;
  padding: 16px;
  border-radius: 8px;
}

.stat-item {
  text-align: center;
}

.stat-label {
  display: block;
  font-size: 12px;
  color: #999;
  margin-bottom: 4px;
}

.stat-value {
  display: block;
  font-size: 16px;
  font-weight: bold;
  color: #333;
}

.card-footer {
  text-align: center;
}

.upgrade-btn,
.renew-btn {
  background: linear-gradient(135deg, #ff6b6b, #ee5253);
  color: white;
  border: none;
  padding: 12px 24px;
  border-radius: 8px;
  font-weight: bold;
  cursor: pointer;
  transition: all 0.3s ease;
  text-decoration: none;
  display: inline-block;
}

.upgrade-btn:hover,
.renew-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 16px rgba(255, 107, 107, 0.3);
}
</style>